package facade

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	command_service "mine-info-server/app/command_service"
	"mine-info-server/app/query_service"
	"mine-info-server/domain/plan-manage-context/service"
	"mine-info-server/global"
	"mine-info-server/infra/database/mysql/plan-manage-context/plan/repo"
	"mine-info-server/interfaces/assembler"
	"mine-info-server/interfaces/request"
	"mine-info-server/utils"
	"mine-info-server/utils/response"
)

/*
计划管理-接口
*/

type PlanManageServer struct {
	queryService   *query_service.PlanManageQueryService
	commandService *command_service.PlanManageCommandService
	assembler      assembler.PlanManageAssembler
}

func NewPlanManageServer() *PlanManageServer {
	return &PlanManageServer{
		queryService: &query_service.PlanManageQueryService{
			PlanManageRepo: repo.NewPlanManageRepo(),
		},
		commandService: &command_service.PlanManageCommandService{
			PlanDomainService: service.NewPlanManageDomainService(
				repo.NewPlanManageRepo()),
			PlanManageRepo: repo.NewPlanManageRepo(),
		}}
}

// CreatePlan 创建计划
// todo 这里有个消息需要做一下
func (p *PlanManageServer) CreatePlan(context *gin.Context) {
	var req *request.CreatePlanRequest
	_ = context.ShouldBindJSON(&req)
	command := p.assembler.ToCommandFromCreateRequest(req)
	if err := p.commandService.CreatePlan(context, command); err != nil {
		global.GVA_LOG.Error("PlanManageServer.CreatePlan 创建失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.Ok(context)
	}
}

// DeletePlan 删除计划
func (p *PlanManageServer) DeletePlan(context *gin.Context) {
	var req *request.IdRequest
	_ = context.ShouldBindJSON(&req)
	command := p.assembler.ToCommandFromDeleteRequest(req)
	if err := p.commandService.DeletePlan(context, command); err != nil {
		global.GVA_LOG.Error("PlanManageServer.DeletePlan 删除失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.Ok(context)
	}
}

// EditPlan 编辑计划
func (p *PlanManageServer) EditPlan(context *gin.Context) {
	var req *request.EditPlanRequest
	_ = context.ShouldBindJSON(&req)
	command := p.assembler.ToCommandFromEditRequest(req)
	if err := p.commandService.EditPlan(context, command); err != nil {
		global.GVA_LOG.Error("PlanManageServer.EditPlan 编辑失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.Ok(context)
	}
}

// GetPlan 查询单个计划
func (p *PlanManageServer) GetPlan(context *gin.Context) {
	var req *request.GetByPlanId
	_ = context.ShouldBindQuery(&req)
	query := p.assembler.ToQueryFromGetPlanRequest(req)
	if res, err := p.queryService.FindPlan(context, query); err != nil {
		global.GVA_LOG.Error("PlanManageServer.GetPlan 查询失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.OkWithData(gin.H{"plan": res}, context)
	}
}

// EditTarget 设置目标
func (p *PlanManageServer) EditTarget(context *gin.Context) {
	var req *request.EditTargetRequest
	_ = context.ShouldBindJSON(&req)
	command := p.assembler.ToCommandFromEditTargetRequest(req)
	if err := p.commandService.EditTarget(context, command); err != nil {
		global.GVA_LOG.Error("PlanManageServer.EditTarget 编辑失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.Ok(context)
	}
}

// GetPlanDetail 查询计划详情
func (p *PlanManageServer) GetPlanDetail(context *gin.Context) {
	var req *request.GetByPlanId
	_ = context.ShouldBindQuery(&req)
	query := p.assembler.ToQueryFromGetPlanDetailRequest(req)
	if res, err := p.queryService.FindPlanDetail(context, query); err != nil {
		global.GVA_LOG.Error("PlanManageServer.GetPlanDetail 查询失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.OkWithData(gin.H{"plan_detail": res}, context)
	}
}

// GetPlanList 获取分页计划列表
// todo 计划状态展示有问题，当天显示超时
func (p *PlanManageServer) GetPlanList(context *gin.Context) {
	var req request.PageInfo
	_ = context.ShouldBindQuery(&req)
	query := p.assembler.ToQueryFromGetPlanList(req)
	if res, total, err := p.queryService.FindPlanMultiLevelList(context, query); err != nil {
		global.GVA_LOG.Error("PlanManageServer.GetPlanList 查询失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     res,
			Total:    total,
			Page:     req.Page,
			PageSize: req.PageSize,
		}, "获取成功", context)
	}
}

// GetAllPlan 获取所有计划的计划名及计划id
func (p *PlanManageServer) GetAllPlan(context *gin.Context) {
	userID := utils.GetUserID(context)
	if res, err := p.queryService.GetAllPlan(context, userID); err != nil {
		global.GVA_LOG.Error("PlanManageServer.GetAllPlan 查询失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.OkWithData(res, context)
	}
}

// GeneratePlanReport 生成计划报告
func (p *PlanManageServer) GeneratePlanReport(context *gin.Context) {
	var req *request.GetByPlanId
	_ = context.ShouldBindQuery(&req)
	query := p.assembler.ToQueryFromGenerateReportRequest(req)
	if res, err := p.queryService.GeneratePlanReport(context, query); err != nil {
		global.GVA_LOG.Error("PlanManageServer.GeneratePlanReport 生成失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.OkWithData(res, context)
	}
}

// SubmitPlanReport 提交报告
func (p *PlanManageServer) SubmitPlanReport(context *gin.Context) {
	var req *request.SubmitPlanReportRequest
	_ = context.ShouldBindJSON(&req)
	command := p.assembler.ToCommandFromSubmitPlanReport(req)
	if err := p.commandService.SubmitPlanReport(context, command); err != nil {
		global.GVA_LOG.Error("PlanManageServer.SubmitPlanReport 提交失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.Ok(context)
	}
}

// FindPlanReport 查看报告
func (p *PlanManageServer) FindPlanReport(context *gin.Context) {
	var req *request.GetByPlanId
	_ = context.ShouldBindQuery(&req)
	query := p.assembler.ToQueryFromFindPlanReport(req)
	if res, err := p.queryService.FindPlanReport(context, query); err != nil {
		global.GVA_LOG.Error("PlanManageServer.FindPlanReport 查看失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.OkWithData(res, context)
	}
}

//
func (p *PlanManageServer) CreateTask(context *gin.Context) {
	var req *request.CreateTaskRequest
	_ = context.ShouldBindJSON(&req)
	command := p.assembler.ToCommandFromCreateTaskRequest(req)
	if err := p.commandService.CreateTask(context, command); err != nil {
		global.GVA_LOG.Error("PlanManageServer.CreateTask 创建失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.Ok(context)
	}
}

func (p *PlanManageServer) DeleteTaskById(context *gin.Context) {
	var req *request.GetById
	_ = context.ShouldBindJSON(&req)
	command := p.assembler.ToCommandFromDeleteTaskById(req)
	if err := p.commandService.DeleteTaskById(context, command); err != nil {
		global.GVA_LOG.Error("PlanManageServer.DeleteTaskById 删除失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.Ok(context)
	}
}

func (p *PlanManageServer) DeleteTaskItemById(context *gin.Context) {
	var req *request.GetById
	_ = context.ShouldBindJSON(&req)
	command := p.assembler.ToCommandFromDeleteTaskItemById(req)
	if err := p.commandService.DeleteTaskItemById(context, command); err != nil {
		global.GVA_LOG.Error("PlanManageServer.DeleteTaskItemById 删除失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.Ok(context)
	}
}

func (p *PlanManageServer) GetTaskItemById(context *gin.Context) {
	var req *request.GetById
	_ = context.ShouldBindQuery(&req)
	query := p.assembler.ToQueryFromGetTaskItemById(req)
	if res, err := p.queryService.GetTaskItemById(context, query); err != nil {
		global.GVA_LOG.Error("PlanManageServer.GetTaskItemById 查询失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.OkWithData(gin.H{"task": res}, context)
	}
}

// GetTaskList 获取计划中的待办分页
func (p *PlanManageServer) GetTaskList(context *gin.Context) {
	var req *request.GetTaskListRequest
	_ = context.ShouldBindQuery(&req)
	query := p.assembler.ToQueryFromGetTaskList(req)
	if res, total, err := p.queryService.GetTaskList(context, query); err != nil {
		global.GVA_LOG.Error("PlanManageServer.GetTaskList 查询失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     res,
			Total:    total,
			Page:     req.Page,
			PageSize: req.PageSize,
		}, "获取成功", context)
	}
}

// EditTaskItem 编辑待办项
func (p *PlanManageServer) EditTaskItem(context *gin.Context) {
	var req *request.EditTaskItemRequest
	_ = context.ShouldBindJSON(&req)
	command := p.assembler.ToCommandFromEditTaskItem(req)
	if err := p.commandService.EditTaskItemById(context, command); err != nil {
		global.GVA_LOG.Error("PlanManageServer.EditTaskItem 编辑失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.Ok(context)
	}
}

// FinishTaskItem 完成待办项
func (p *PlanManageServer) FinishTaskItem(context *gin.Context) {
	var req *request.GetById
	_ = context.ShouldBindJSON(&req)
	command := p.assembler.ToCommandFromFinishTaskItem(req)
	if err := p.commandService.FinishTaskItem(context, command); err != nil {
		global.GVA_LOG.Error("PlanManageServer.FinishTaskItem 完成失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.Ok(context)
	}
}

// SaveQuestion 保存问题集
func (p *PlanManageServer) SaveQuestion(context *gin.Context) {
	var req *request.QuestionRequest
	err := context.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), context)
		return
	}
	command := p.assembler.ToCommandFromSaveQuestionRequest(req)
	if err := p.commandService.SaveQuestions(context, command); err != nil {
		global.GVA_LOG.Error("PlanManageServer.SaveQuestions 修改失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.Ok(context)
	}
}

// FindOneDayQuestions 查询某天的问题集
func (p *PlanManageServer) FindOneDayQuestions(context *gin.Context) {
	var req *request.OneDayQuestionRequest
	err := context.ShouldBindQuery(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), context)
		return
	}
	query := p.assembler.ToQueryFromFindOneDayQuestions(req)
	if res, err := p.queryService.FindOneDayQuestions(context, query); err != nil {
		global.GVA_LOG.Error("PlanManageServer.FindOneDayQuestions 查询失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.OkWithData(res, context)
	}
}

// SearchQuestions 搜索问题集
func (p *PlanManageServer) SearchQuestions(context *gin.Context) {
	var req *request.SearchQuestionsRequest
	err := context.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), context)
		return
	}
	query := p.assembler.ToQueryFromSearchQuestionRequest(req)
	if res, err := p.queryService.SearchQuestions(context, query); err != nil {
		global.GVA_LOG.Error("PlanManageServer.SearchQuestions 查询失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.OkWithData(res, context)
	}
}

// GetOngoingTasks 获取进行中的待办任务
func (p *PlanManageServer) GetOngoingTasks(context *gin.Context) {
	var req *request.GetOngoingTasksRequest
	err := context.ShouldBindQuery(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), context)
		return
	}
	query := p.assembler.ToQueryFromGetOngoingTasksRequest(req)
	if res, err := p.queryService.GetOngoingTasks(context, query); err != nil {
		global.GVA_LOG.Error("PlanManageServer.GetOngoingTasks 查询失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.OkWithData(res, context)
	}
}

// IsFinishTask 是否完成所有任务
func (p *PlanManageServer) IsFinishTask(context *gin.Context) {
	var req *request.GetByPlanId
	_ = context.ShouldBindQuery(&req)
	query := p.assembler.ToQueryFromIsFinishTaskRequest(req)
	if res, err := p.queryService.IsFinishTask(context, query); err != nil {
		global.GVA_LOG.Error("PlanManageServer.IsFinishTask 查询失败", zap.Error(err))
		response.FailWithMessage(err.Error(), context)
	} else {
		response.OkWithData(res, context)
	}
}
